package org

import (
	"dhfshop/models/org"
	"time"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

// 分销商管理
type PartnerController struct {
	base.BaseController
}

func (this *PartnerController) Get() {
	curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if curRoleId == 4 {
	    tmpPartnerId, err := this.GetInt("partner_id")
		if err != nil {
			this.ServeError(err.Error())
			return
		}
		if tmpPartnerId == 0 {
		    this.ServeError("partner_id is null")
			return
		}
		partnerId = int64(tmpPartnerId)
	}
	partner, err := org.GetPartnerById(int64(partnerId))
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	this.Data["json"] = partner
	this.ServeJSON()
}

func (this *PartnerController) Post() {
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	var v org.ValuePartner
	valid := validation.Validation{}
	this.ParseForm(&v)
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	v.CreatorId = int64(curUserId)
	partnerId, err := org.AddPartner(&v)
	if err != nil {
        this.ServeError(err.Error())
		return
    }
	rsp := struct {
		PartnerId int64        `json:"partner_id"`
	    Created   time.Time    `json:"created"`
	}{PartnerId: partnerId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *PartnerController) Put() {
	curPartnerId := this.Ctx.Input.GetData("curCompanyId").(int)
	//curUserId := this.Ctx.Input.GetData("curUserId").(int)
	v := org.ValuePartner{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.PartnerId, "partner_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	if curPartnerId != int(v.PartnerId) {
	    this.ServeError("cur user partner_id is not invalid")
		return
	}
	/*
	//只能创建者修改
	oldPartner, err := org.GetPartnerById(v.PartnerId)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if curUserId != int(oldPartner.CreatorId) {
	    this.ServeError("cur user is not creator")
		return
	}
	*/
	excludeArr := []string{"partner_id","industry_code","pay_way_code","activated","serve_start_at","serve_end_at","created_by","created_at","updated_at","creator_id"}
	updateFields, err := this.GetUpdateFields(v, "", "form", excludeArr)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if err := org.UpdatePartnerById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		PartnerId  int64       `json:"partner_id"`
		Modified   time.Time   `json:"modified"`
	}{PartnerId:v.PartnerId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *PartnerController) Delete() {
	partner_id, _ := this.GetInt("partner_id")
	if err := org.DeletePartner(int64(partner_id)); err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		PartnerId  int       `json:"partner_id"`
		Modified   time.Time `json:"modified"`
	}{PartnerId:partner_id, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

        
